<?php
// Make theme available for translation
// Translations can be filed in the /languages/ directory
load_theme_textdomain( 'shape', TEMPLATEPATH . '/languages' );

$locale = get_locale();
$locale_file = TEMPLATEPATH . "/languages/$locale.php";
if ( is_readable($locale_file) )
	require_once($locale_file);

// Block comments rss link to be displayed by wp_header() function
function remove_comments_rss( $for_comments ) {
    return;
}
add_filter('post_comments_feed_link','remove_comments_rss');	

//enable post thumnail
if ( function_exists( 'add_theme_support' ) ) {
	add_theme_support( 'post-thumbnails' );
    set_post_thumbnail_size( 150, 150 ); // default Post Thumbnail dimensions   
}

//Add a new image size category 
//Existing categories: thumb, thumbnail, medium, large, post-thumbnail
if ( function_exists( 'add_image_size' ) ) { 
	add_image_size( 'thumb90x90', 90, 90 );
	add_image_size( 'thumb100x100', 100, 100 );
	add_image_size( 'thumb120x120', 120, 120 );
	add_image_size( 'thumb150x150', 150, 150 );
	add_image_size( 'thumb200x200', 200, 200 );
	add_image_size( 'thumb250x250', 250, 250 );
	add_image_size( 'thumb270x270', 270, 270 );
	add_image_size( 'thumb320x234', 320, 234 );
}

// retreives the categories for a post ordered by id
function custom_post_categories( $post_id, $separator = ' &gt; ' )
{	
	global $wp_rewrite;
	$rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
	$post_categories = wp_get_post_categories( $post_id, array('orderby' => 'id') );
	$thelist = ''; $i = 0;
	
	foreach($post_categories as $c){
		if ( 0 < $i )
			$thelist .= $separator;
		
		$category = get_category( $c );
		$catLink = get_category_link( $category->term_id );
		$catTitle = esc_attr( sprintf( __( "Vezi toate articolele din %s" ), $category->name ) );
		
		if ($category->parent > 0){
			$catLink = esc_url( get_permalink( get_page_by_title( $category->cat_name, 'OBJECT', 'post' ) ) );
			$catTitle = $category->name;
		}
		
		$thelist .= '<a href="'.$catLink.'" title="'.$catTitle.'" '.$rel.'>'.$category->name.'</a>';
		++$i;
	}
	
	echo $thelist;
}

function indefinite_article($post_id){
	$postTags = get_the_tags($post_id);
	$article = "un (o)";
	if ($postTags) {
	  foreach($postTags as $tag) {
		if ($tag->name == "Male"){
			$article = "un";
			break;
		}
		else if ($tag->name == "Female"){
			$article = "o";
			break;
		}
	  }
	}
	
	return $article;
}

//Limit the words from an excerpt
function string_limit_words($string, $word_limit)
{
  $words = explode(' ', $string, ($word_limit + 1));
  if(count($words) > $word_limit)
  array_pop($words);
  return implode(' ', $words);
}

// Remove http or https and www from url
function remove_http($url = ''){
	return(str_replace(array('http://','https://','www.'), '', $url));
}

// Used to display social url's with a char limit
function format_social_url($url = '', $chars = 25){
	$url = strtolower(remove_http($url));
	
	if (strlen($url) <= $chars)
		return $url;
	else
		return (substr($url, 0, $chars)."...");
}

// Get the page number
function get_page_number() {
    if (get_query_var('paged')) {
        print ' | ' . __( 'Page ' , 'shape') . get_query_var('paged');
    }
} // end get_page_number


// For category lists on category archives: Returns other categories except the current one (redundant)
function cats_meow($glue) {
	$current_cat = single_cat_title( '', false );
	$separator = "\n";
	$cats = explode( $separator, get_the_category_list($separator) );
	foreach ( $cats as $i => $str ) {
		if ( strstr( $str, ">$current_cat<" ) ) {
			unset($cats[$i]);
			break;
		}
	}
	if ( empty($cats) )
		return false;

	return trim(join( $glue, $cats ));
} // end cats_meow


// For tag lists on tag archives: Returns other tags except the current one (redundant)
function tag_ur_it($glue) {
	$current_tag = single_tag_title( '', '',  false );
	$separator = "\n";
	$tags = explode( $separator, get_the_tag_list( "", "$separator", "" ) );
	foreach ( $tags as $i => $str ) {
		if ( strstr( $str, ">$current_tag<" ) ) {
			unset($tags[$i]);
			break;
		}
	}
	if ( empty($tags) )
		return false;

	return trim(join( $glue, $tags ));
} // end tag_ur_it


// Register widgetized areas
function theme_widgets_init() {
	// Area 1
  register_sidebar( array (
  'name' => 'Primary Widget Area',
  'id' => 'primary-widget-area',
  'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
  'after_widget' => "</li>",
	'before_title' => '<h3 class="widget-title">',
	'after_title' => '</h3>',
  ) );

	// Area 2
  register_sidebar( array (
  'name' => 'Secondary Widget Area',
  'id' => 'secondary-widget-area', 
  'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
  'after_widget' => "</li>",
	'before_title' => '<h3 class="widget-title">',
	'after_title' => '</h3>',
  ) );
} // end theme_widgets_init

add_action( 'init', 'theme_widgets_init' );


// Pre-set Widgets
$preset_widgets = array (
	'primary-aside'  => array( 'search', 'pages', 'categories', 'archives' ),
	'secondary-aside'  => array( 'links', 'meta' )
);
if ( isset( $_GET['activated'] ) ) {
	update_option( 'sidebars_widgets', $preset_widgets );
}


// Check for static widgets in widget-ready areas
function is_sidebar_active( $index ){
  global $wp_registered_sidebars;

  $widgetcolums = wp_get_sidebars_widgets();
		 
  if ($widgetcolums[$index]) return true;
  
	return false;
} // end is_sidebar_active


// Produces an avatar image with the hCard-compliant photo class
function commenter_link() {
	$commenter = get_comment_author_link();
	if ( ereg( '<a[^>]* class=[^>]+>', $commenter ) ) {
		$commenter = ereg_replace( '(<a[^>]* class=[\'"]?)', '\\1url ' , $commenter );
	} else {
		$commenter = ereg_replace( '(<a )/', '\\1class="url "' , $commenter );
	}
	$avatar_email = get_comment_author_email();
	$avatar = str_replace( "class='avatar", "class='photo avatar", get_avatar( $avatar_email, 80 ) );
	echo $avatar . ' <span class="fn n">' . $commenter . '</span>';
} // end commenter_link


// Custom callback to list comments in the shape style
function custom_comments($comment, $args, $depth) {
  $GLOBALS['comment'] = $comment;
	$GLOBALS['comment_depth'] = $depth;
  ?>
  	<li id="comment-<?php comment_ID() ?>" class="<?php comment_class() ?>">
  		<div class="comment-author vcard"><?php commenter_link() ?></div>
  		<div class="comment-meta"><?php printf(__('Posted %1$s at %2$s <span class="meta-sep">|</span> <a href="%3$s" title="Permalink to this comment">Permalink</a>', 'shape'),
  					get_comment_date(),
  					get_comment_time(),
  					'#comment-' . get_comment_ID() );
  					edit_comment_link(__('Edit', 'shape'), ' <span class="meta-sep">|</span> <span class="edit-link">', '</span>'); ?></div>
  <?php if ($comment->comment_approved == '0') _e("\t\t\t\t\t<span class='unapproved'>Your comment is awaiting moderation.</span>\n", 'shape') ?>
          <div class="comment-content">
      		<?php comment_text() ?>
  		</div>
		<?php // echo the comment reply link with help from Justin Tadlock http://justintadlock.com/ and Will Norris http://willnorris.com/
			if($args['type'] == 'all' || get_comment_type() == 'comment') :
				comment_reply_link(array_merge($args, array(
					'reply_text' => __('Reply','shape'), 
					'login_text' => __('Log in to reply.','shape'),
					'depth' => $depth,
					'before' => '<div class="comment-reply-link">', 
					'after' => '</div>'
				)));
			endif;
		?>
<?php } // end custom_comments


// Custom callback to list pings
function custom_pings($comment, $args, $depth) {
       $GLOBALS['comment'] = $comment;
        ?>
    		<li id="comment-<?php comment_ID() ?>" class="<?php comment_class() ?>">
    			<div class="comment-author"><?php printf(__('By %1$s on %2$s at %3$s', 'shape'),
    					get_comment_author_link(),
    					get_comment_date(),
    					get_comment_time() );
    					edit_comment_link(__('Edit', 'shape'), ' <span class="meta-sep">|</span> <span class="edit-link">', '</span>'); ?></div>
    <?php if ($comment->comment_approved == '0') _e('\t\t\t\t\t<span class="unapproved">Your trackback is awaiting moderation.</span>\n', 'shape') ?>
            <div class="comment-content">
    			<?php comment_text() ?>
			</div>
<?php } // end custom_pings


// Adding the Theme options page in Admin section / Settings menu
add_action('admin_menu', 'esf_admin_menu');
function esf_admin_menu() 
{
	add_options_page('Theme Options', 'Theme Options', 'manage_options', 'esfaturi-theme-options', 'esf_admin_options');
}

function esf_admin_options() 
{
	if (!current_user_can('manage_options')) {
		wp_die( __('You do not have sufficient permissions to access this page.') );
	}
	
	// Home page
	$esf_home_no_of_articles = $_POST['esf_home_no_of_articles'];
	
	// Social Optiond
	$esf_facebook_url = $_POST['esf_facebook_url'];
	$esf_twitter_url = $_POST['esf_twitter_url'];
	$esf_google_plus_url = $_POST['esf_google_plus_url'];
	
	// Footer options
	$esf_footer_message = $_POST['esf_footer_message'];
	
	// Header options (SEO)
	$esf_site_description = $_POST['esf_site_description'];

    if (isset($_POST['info_update'])) {
		update_option('esf_home_no_of_articles', (string)$_POST['esf_home_no_of_articles']);
		update_option('esf_facebook_url', (string)$_POST['esf_facebook_url']);
		update_option('esf_twitter_url', (string)$_POST['esf_twitter_url']);
		update_option('esf_google_plus_url', (string)$_POST['esf_google_plus_url']);
		update_option('esf_footer_message', (string)$_POST['esf_footer_message']);
		update_option('esf_site_description', (string)$_POST['esf_site_description']);
	}
	else {
		$esf_home_no_of_articles = get_option('esf_home_no_of_articles');
		$esf_facebook_url = get_option('esf_facebook_url');		
		$esf_twitter_url = get_option('esf_twitter_url');
		$esf_google_plus_url = get_option('esf_google_plus_url');
		$esf_footer_message = get_option('esf_footer_message');
		$esf_site_description = get_option('esf_site_description');
	}
	?>
	
	<div class=wrap>
		<form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
			<input type="hidden" name="info_update" id="info_update" value="true" />
			<u><h2>eSfaturi Theme Options</h2></u>

			<div id="poststuff" style="height:500px;" class="metabox-holder has-right-sidebar">
				<div style="width:70%;">
					<div class="postbox">
						<h3>eSfaturi - Options</h3>
						<table class="form-table">
							<tr valign="top" class="alternate">
								<th scope="row"><label>Home page</label></th>
								<td colspan="2">
									<input name="esf_home_no_of_articles" type="text" size="50" value="<?php echo get_option('esf_home_no_of_articles'); ?>" /> No. of posts (except slideshow)
								</td>
							</tr>
							<tr valign="top" class="alternate">
								<th scope="row"><label>Header options (SEO)</label></th>
								<td colspan="2">
									<textarea name="esf_site_description" rows="3" cols="47" style="vertical-align:top;"><?php echo get_option('esf_site_description'); ?></textarea> Site description (meta tag)
								</td>
							</tr>
							<tr valign="top" class="alternate">
								<th scope="row"><label>Social</label></th>
								<td colspan="2">
									<input name="esf_facebook_url" type="text" size="50" value="<?php echo get_option('esf_facebook_url'); ?>" /> URL of the Facebook page

									<br>

									<input name="esf_twitter_url" type="text" size="50" value="<?php echo get_option('esf_twitter_url'); ?>" /> URL of the Twitter page

									<br>

									<input name="esf_google_plus_url" type="text" size="50" value="<?php echo get_option('esf_google_plus_url'); ?>" /> URL of the Google + page
								</td>
							</tr>
							<tr valign="top" class="alternate">
								<th scope="row"><label>Footer</label></th>
								<td colspan="2">
									<textarea name="esf_footer_message" rows="3" cols="47" style="vertical-align:top;"><?php echo get_option('esf_footer_message'); ?></textarea> Footer copyright message
								</td>
							</tr>
						</table>
					</div>

					<div class="submit">
						<input type="submit" name="option_value" class="button-primary" value="<?php _e( 'Update Options' ); ?>" />
					</div>
				</div>
			</div>
		</form>
	</div>
	
	<?php
}
// End of adding the Theme options page in Admin section / Settings menu

?>